<!-- 机构权限 -->
<template>
    <div class="leftRightContent">
        <div class="leftContent" style="width: 700px; padding: 10px">
            <Tabs :model-value="type" @on-click="tabClick">
                <TabPane label="企业组织" name="enterprise">
                    <selectEnterpriseOrg :height="orgHeight" @handleCurrentChange="handleCurrentChange"></selectEnterpriseOrg>
                </TabPane>
                <TabPane label="个人组织" name="person">
                    <selectPersonOrg :height="orgHeight" @handleCurrentChange="handleCurrentChange"></selectPersonOrg>
                </TabPane>
            </Tabs>
        </div>
        <div class="rightContent" style="width: calc(100% - 710px); padding: 10px">
            <navigation-bar :customTitle="curTitle">
                <div class="searchContent">
                    <label class="red"> 【选择菜单的时候，请一定要选择父级，否则左侧菜单将不会显示出来】 </label>
                    <custom-button type="save" @saveClick="saveClick"></custom-button>
                </div>
            </navigation-bar>
            <div style="padding: 0 10px; overflow-y: auto" :style="{height: secondTableHeight + 'px'}">
                <Tree :data="dataSource" show-checkbox :check-strictly="false" @on-check-change="checkChange"></Tree>
            </div>
        </div>
    </div>
</template>

<script>
import arrowLeft from '@/components/customComponents/arrowLeftOrg'
import navigationBar from '@/components/customComponents/managementCenter/navigationBarCustomTitle'
import customButton from '@/components/customComponents/customForm/customButton'
import {Utility} from '@/libs/utility'
import {PopupCommon} from '@/libs/popupCommon'
import selectEnterpriseOrg from '@/components/customComponents/managementCenter/selectEnterpriseOrg'
import selectPersonOrg from '@/components/customComponents/managementCenter/selectPersonOrg'
import {AjaxCommon} from '@/libs/apiCommon'
export default {
    name: 'eCBAnalysis',
    components: {arrowLeft, navigationBar, customButton, selectEnterpriseOrg, selectPersonOrg},
    data() {
        return {
            type: 'enterprise',
            orgHeight: 0,
            secondTableHeight: 0,
            curTitle: '机构权限设置',
            tabItem: {
                type: 'enterprise',
                itemInfo: {}
            },
            dataSource: [],
            selectData: []
        }
    },
    computed: {},
    created() {},
    methods: {
        checkChange(e) {
            this.selectData = e
        },
        saveClick() {
            if (this.tabItem.itemInfo?.orgId) {
                if (this.selectData.length > 0) {
                    const sysMenu = this.selectData.filter((t) => t.attributes == 'sys')
                    const menuMenu = this.selectData.filter((t) => t.attributes == 'menu')
                    if (sysMenu.length == 0) {
                        PopupCommon.notify('info', '请勾选至少一个子系统')
                        return
                    }
                    if (menuMenu.length == 0) {
                        PopupCommon.notify('info', '请勾选至少一个菜单')
                        return
                    }
                    const params = {
                        orgid: this.tabItem.itemInfo?.orgId,
                        sysIds: sysMenu?.map((t) => t.id).toString(),
                        sysNames: sysMenu?.map((t) => t.text).toString(),
                        menuIds: menuMenu.map((t) => t.id).toString()
                    }

                    AjaxCommon(`/api/OrgSysManage/SaveOrgSys`, 'post', params).then((res) => {
                        if (res?.data?.Code == 200) {
                            this.dataSource = []
                            PopupCommon.notify('success', '保存成功')
                            this.getAllMenuHasSys()
                        } else {
                            const msg = res?.data?.Message
                            if (msg) {
                                PopupCommon.notify('error', msg)
                            }
                        }
                    })
                } else {
                    PopupCommon.notify('info', '请选择菜单')
                }
            } else {
                PopupCommon.notify('info', '请选择左侧的组织机构')
            }
        },
        tabClick(e) {
            this.curTitle = `机构权限设置`
            this.tabItem = {
                type: e,
                itemInfo: {}
            }
        },
        getHeight() {
            this.orgHeight = document.body.clientHeight - 290
            this.secondTableHeight = document.body.clientHeight - 190
        },
        handleCurrentChange(d) {
            this.tabItem.itemInfo = d
            const name = this.tabItem.type == 'enterprise' ? d.fullName : d.URealName

            this.curTitle = `机构权限设置【${name}】`
            this.dataSource = []
            this.getAllMenuHasSys()
        },
        async getAllMenuHasSys() {
            const orgId = this.tabItem.itemInfo.orgId
            let powerList = []
            // 获取当前组织下的根据组织机构获取菜单树
            const params = `/api/OrgSysManage/GetSysTreeByOrg?orgid=${orgId}`
            powerList = await AjaxCommon(params, 'get', null)
            let powerData = powerList?.data?.Message
            if (powerData && powerData.length > 0) {
                powerList = Utility.arrayFlat(powerData, (t) => t.children)
                powerList = powerList?.filter((t) => typeof t.checked == 'undefined' || t.checked).map((t) => t.id)
            }
            const allSysData = await AjaxCommon(`/api/Menu/GetSysMenuData`, 'get', null)
            const listData = allSysData?.data?.Message ?? []
            let treeDataList = []
            Utility.createOrgMenuTreeData(listData, treeDataList, powerList)
            this.dataSource = treeDataList
        }
    },
    mounted() {
        this.$nextTick(() => {
            this.getHeight()
        })

        /*窗口自适应，关键代码*/
        window.onresize = () => {
            this.getHeight()
        }
    }
}
</script>

<style lang="scss" scoped>
.red {
    margin-right: 10px;
    font-size: 14px;
    color: red;
    font-weight: bold;
}
</style>
